1713E - Cross Swapping - CodeForces Solution


2-sat data structures dsu greedy matrices *2400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
enum{N=2013};
int n,a[N][N],f[N],T;
int fd(int x){return x==f[x]?x:f[x]=fd(f[x]);}
void mg(int x,int y){f[fd(x)]=fd(y);}
int main(){
ios::sync_with_stdio(0);
cin>>T;while(T--){
cin>>n,iota(f,f+2*n,0);
for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>a[i][j];
for(int i=0;i<n;i++)for(int j=i;j<n;j++)
if(a[i][j]<a[j][i])(fd(i)^fd(j+n))?mg(i,j),mg(i+n,j+n):swap(a[i][j],a[j][i]);
else if(a[i][j]>a[j][i]&&fd(i)!=fd(j))mg(j,i+n),mg(i,j+n),swap(a[i][j],a[j][i]);
for(int i=0;i<n;i++)for(int j=0;j<n;j++)cout<<a[i][j]<<" \n"[j==n-1];
}
}


Comments

Submit
0 Comments
More Questions

1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke